#pragma once

#include <iostream>
using std::cout;
using std::endl;

template<typename T>
class Singleton
{
public:
    template <class... Args>
    static T * getInstance(Args... args)
    {
        if(nullptr == _pInstance){
            _pInstance = new T(args...);
        }
        return _pInstance;
    }

private:
    class AutoRelease{
    public:
        ~AutoRelease()
        {
            if(_pInstance){
                delete _pInstance;
                _pInstance = nullptr;
            }
        }
    };

    Singleton() = default;
    Singleton(const Singleton &) = delete;
    Singleton & operator=(const Singleton &) = delete;

    ~Singleton()
    {
        cout << "~Singleton()" << endl;
    }

private:
    static T * _pInstance;
    static AutoRelease _ar; // 静态对象成员 创建在全局静态区
};
template <class T> 
T * Singleton<T>::_pInstance = nullptr;

template <class T> 
typename Singleton<T>::AutoRelease Singleton<T>::_ar;